<template>
  <section>
    <div class="model myfile">
      <img :src="IMGBASE64.WODEWENJIAN2X" class="pink">
      <span class="title">部门管理</span>
      <div class="kong"></div>
      <div class="t_title">
        <img>
        <span class="f-blue">{{schoolName}}</span>
      </div>
      <div class="tree">
          <el-tree
          ref="tree"
          :data="data"
          :props="defaultProps"
          accordion
          node-key="id">
        </el-tree>
      </div>
    </div>
    


    <div class="dright">
      <div class="row">
        <span class="dspan">学校选择:</span>
        <el-select v-model="subjectSelect" @change="schoolChoose" size="small">
          <el-option v-for="(item, key) in subjectOption" :label="item" :value="key"></el-option>
        </el-select>
      </div>
      <div class="kong"></div>
      <div class="t_title">
        <img>
        <span class="f-blue">学校组织架构</span>
      </div>

      <div class="tree">
        <el-tree
        :default-checked-keys="sList"
        :expand-on-click-node="true"
        @check-change="checkChange"
        :filter-node-method="filterNode"
        show-checkbox
        ref="tree2"
        :data="data2"
        :props="defaultProps"
        accordion
        node-key="id"
        :default-expanded-keys="[100]">
      </el-tree>
      <div class="button">
        <button class="btn btn-danger" @click="tipshow" :disabled="disabled">确认提交</button>
      </div>
      <!-- <button class="btn btn-danger">确认提交</button> -->
    </div>
    </div>

    <div class="up" v-show="upShow">
      <img :src="IMGBASE64.GREENXINZENG2X" class="green">
      <span class="title">新建</span>
      <el-button icon="el-iocn-close" class="close" @click="upshow()"></el-button>
      <header>
        <span class="header-title">基本信息</span>
      </header>
      <div class="form">
        <div class="firstLine">
          <span>部门名称:</span>
          <input type="text" class="form-longText">
        </div>
        <div class="secondLine">
          <span>创建角色:</span>
          <ul>
            <li><span>角色1</span><input type="text" class="form-text"></li>
            <li><span>角色2</span><input type="text" class="form-text"></li>
            <li><span>角色3</span><input type="text" class="form-text"></li>
          </ul>
        </div>
      </div>
      <div class="bottom">
        <div class="right">
          <el-button type="success" size="small">确定</el-button>
        </div>
      </div>
    </div>

    <div class="tipUp" v-show="tipShow">
      <div class="row">
        <span class="err">如若提交,不可更改</span>
      </div>
      <div class="row">
        <el-button type="danger" @click="subList" size="small">确定</el-button>
        <el-button @click="tipshow" size="small">取消</el-button>
      </div>
    </div>

    <transition name="modal-shadow">
      <div class="modal-backdrop" v-show="anShow"></div>
    </transition>
  </section>
</template>

<script>
// let code = 1000
export default {
  data () {
    return {
      disabled: false,
      sList: [],
      tipShow: false,
      schoolId: null,
      schoolList: [],
      schoolName: '学校组织架构',
      expand: [100],
      subjectSelect: null,
      subjectOption: [],
      upShow: false,
      anShow: false,
      data: [],
      data2: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  mounted () {
    this.$http.post('http://119.23.36.128:8020/yxt-web/sys/school/manage/schoolList', {}, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
      if (response.body.code === '000') {
        let asdf = {}
        for (var i = 0; i < response.data.data.length; i++) {
          asdf[response.data.data[i].id] = response.data.data[i].schoolName
        }
        this.subjectOption = asdf
      } else if (response.body.code === '887') {
        this.$router.push('/')
      } else if (response.body.code === '889') {
        this.$router.push('/')
      }
    })
    // this.$refs.tree.$el.firstElementChild.firstElementChild.lastElementChild.lastElementChild.childNodes[1].style.display = 'none'
    // this.$refs.tree.$el.firstElementChild.firstElementChild.lastElementChild.lastElementChild.childNodes[2].style.display = 'none'
  },
  methods: {
    getCheckedNodes () {},
    tipshow () {
      if (this.subjectSelect !== null) {
        this.tipShow = !this.tipShow
        this.anShow = !this.anShow
      }
    },
    checkChange (leafOnly) {
      this.schoolList = []
      let asdf = []
      for (var i = 0; i < this.$refs.tree2.getCheckedNodes(leafOnly).length; i++) {
        this.schoolList.push({id: null})
        this.schoolList[i].id = this.$refs.tree2.getCheckedNodes(leafOnly)[i].id
        if (asdf.indexOf(this.$refs.tree2.getCheckedNodes(leafOnly)[i].parentId) < 0) {
          asdf.push(this.$refs.tree2.getCheckedNodes(leafOnly)[i].parentId)
        }
      }
      for (var a = 0; a < asdf.length; a++) {
        this.schoolList.push({id: asdf[a]})
      }
    },
    subList () {
      if (this.subjectSelect != null) {
        this.$http.post('http://119.23.36.128:8020/yxt-web/sys/depart/manage/initCreate', {
          'schoolId': this.subjectSelect,
          'schoolList': this.schoolList
        }, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
          if (response.body.code === '000') {
            this.$http.post('http://119.23.36.128:8020/yxt-web/sys/depart/manage/query', {
              'schoolId': this.subjectSelect
            }, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
              if (response.body.code === '000') {
                this.data = response.data.data.schoolList
                if (this.data != null) {
                  this.disabled = true
                  let lalala = []
                  this.sList = []
                  for (var h = 0; h < response.data.data.schoolList.length; h++) {
                    this.data[h].label = this.data[h].departName
                    this.data[h].children = []
                    this.data[h].children = this.data[h].subList
                    lalala.push(response.data.data.schoolList[h].departId)
                    for (var z = 0; z < this.data[h].children.length; z++) {
                      this.data[h].children[z].label = this.data[h].children[z].position
                      lalala.push(response.data.data.schoolList[h].children[z].positionId)
                    }
                  }
                  for (var e = 0; e < lalala.length - 1; e++) {
                    this.sList.push(lalala[e])
                  }
                  // console.log(this.sList)
                } else {
                  this.disabled = false
                  this.data = []
                  this.sList = []
                }
                this.data2 = response.data.data.sysList
                for (var i = 0; i < response.data.data.sysList.length; i++) {
                  this.data2[i].label = this.data2[i].depart
                  this.data2[i].children = []
                  this.data2[i].children = this.data2[i].subList
                  for (var a = 0; a < this.data2[i].children.length; a++) {
                    this.data2[i].children[a].label = this.data2[i].children[a].position
                  }
                }
              } else if (response.body.code === '887') {
                this.$router.push('/')
              } else if (response.body.code === '889') {
                this.$router.push('/')
              }
            })
          } else if (response.body.code === '887') {
            this.$router.push('/')
          } else if (response.body.code === '889') {
            this.$router.push('/')
          }
        })
      }
      this.tipShow = !this.tipShow
      this.anShow = !this.anShow
    },
    schoolChoose () {
      if (this.subjectSelect != null) {
        this.schoolName = this.subjectOption[this.subjectSelect] + '组织架构'
        this.$http.post('http://119.23.36.128:8020/yxt-web/sys/depart/manage/query', {
          'schoolId': this.subjectSelect
        }, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
          if (response.body.code === '000') {
            this.data = response.data.data.schoolList
            if (this.data !== null) {
              this.disabled = true
              console.log(this.data)
            } else if (response.body.code === '887') {
              this.$router.push('/')
            } else if (response.body.code === '889') {
              this.$router.push('/')
            }
            if (this.data != null) {
              let lalala = []
              this.sList = []
              for (var h = 0; h < response.data.data.schoolList.length; h++) {
                this.data[h].label = this.data[h].departName
                this.data[h].children = []
                this.data[h].children = this.data[h].subList
                lalala.push(response.data.data.schoolList[h].departId)
                for (var z = 0; z < this.data[h].children.length; z++) {
                  this.data[h].children[z].label = this.data[h].children[z].position
                  lalala.push(response.data.data.schoolList[h].children[z].positionId)
                }
              }
              for (var e = 0; e < lalala.length - 1; e++) {
                this.sList.push(lalala[e])
              }
              // console.log(this.sList)
            } else {
              this.data = []
              this.sList = []
            }
            this.data2 = response.data.data.sysList
            for (var i = 0; i < response.data.data.sysList.length; i++) {
              this.data2[i].label = this.data2[i].depart
              this.data2[i].children = []
              this.data2[i].children = this.data2[i].subList
              for (var a = 0; a < this.data2[i].children.length; a++) {
                this.data2[i].children[a].label = this.data2[i].children[a].position
              }
            }
          } else if (response.body.code === '887') {
            this.$router.push('/')
          } else if (response.body.code === '889') {
            this.$router.push('/')
          }
        })
      }
    },
    mover (ev) {
      ev.path[2].lastElementChild.style.opacity = 0
    },
    mout (ev) {
      ev.path[2].lastElementChild.style.opacity = 1
      // console.log(ev)
    },
    // nodeClick () {
    //   for (var i = 0; i < this.$refs.tree.$el.lastElementChild.lastElementChild.childNodes.length; i++) {
    //     for (var a = 0; a < this.$refs.tree.$el.lastElementChild.lastElementChild.childNodes[i].lastElementChild.childNodes.length; a++) {
    //       this.$refs.tree.$el.lastElementChild.lastElementChild.childNodes[i].lastElementChild.childNodes[a].firstElementChild.lastElementChild.lastElementChild.firstElementChild.style.display = 'none'
    //     }
    //   }
    //   // console.log(this.$refs.tree)
    // },
    append (store, data) {
      // store.append({ code: code++, label: 'testtest', children: [] }, data)
      this.upShow = true
      this.anShow = true
    },
    change (store, data) {
      // store.append({ code: code++, label: 'testtest', children: [] }, data)
      this.upShow = true
      this.anShow = true
    },
    remove (store, data) {
      store.remove(data)
    },
    // renderContent (h, { node, data, store }) {
    //   return (
    //     <span>
    //       <span>
    //         <span on-click={ ($event) => this.mout($event) } on-dblclick={ ($event) => this.mover($event) }>{node.label}</span>
    //       </span>
    //       <span class="span" style="float: right; margin-right: 20px">
    //         <el-button size="mini" on-click={ () => this.append(store, data) }>新建子集</el-button>
    //         <el-button type="warning" size="mini" on-click={ () => this.change(store, data) }>修改</el-button>
    //         <el-button type="danger" size="mini" on-click={ () => this.remove(store, data) }>删除</el-button>
    //       </span>
    //     </span>
    //   )
    // },
    upshow () {
      this.upShow = !this.upShow
      this.anShow = !this.anShow
    },
    filterNode (value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    }
  }
}
</script>

<style scoped>
  @import '/static/css/material-dashboard.css';
  .myfile{
    width:54% !important;
    min-width: 595.67px !important;
    height: 440px;
  }
  .myfile .el-tree{
    position: relative; left: 42%;
    width: 210.1%;
    border: none;
  }
  .myfile .t_title{
    position: relative; left: 18%;
  }
  .myfile .t_title img{
    float: left;
    width: 20px; height: 20px;
    background: #ccc;
    margin-right: 1%;
  }
  section .line{
    position: absolute; left: 55%; top: 60px;
    width: 1px; height: 56%;
    border-left: 1px dashed #666;
  }
  section .dright{
    position: absolute; right: 2%; top: 51px;
    width: 40%; height: 460px;
    background: #fff;
    border-radius: 4px;
  }
  section .dright .row{
    margin-top: 20px;
  }
  section .dright .t_title{
    margin-left: 6%;
  }
  section .dright .t_title img{
    float: left;
    width: 20px; height: 20px;
    background: #ccc;
    margin-right: 1%;
  }
  section .dright .dspan{
    margin-left: 6%;
  }
  section .dright select{
    width: 54%;
    border: none;
    border-bottom: 1px solid #ccc;
  }
  section .dright .t-btn{
    position: relative; left: 34%; top: 14px;
  }
  section .dright .tree{
    position: relative; left: 6%; top: 10px;
    width: 90%;
  }
  section .tree{
    position: relative; left: 2%; top: 14px;
  }
  div[data-v-22bf8d34] ul
  {
    position: relative;
    left: 12%;
  }
  div[data-v-22bf8d34]
  {
    padding-bottom: 10px;
  }
  .el-upload--picture-card
  {
    margin-left: 9%; 
    margin-top: 40px;
    width: 80px; height: 80px;
  }
  .el-upload--picture-card i
  {
    position: relative; top: -28px;
  }
  .tree{
    position: relative; left: 60%;
    width: 40%;
  }
  .span{
    opacity: 0;
  }

  .up{
    width: 60%;
    position: absolute;
    top: 10%;
    left: 25%;
    background: #FFF;
    padding: 20px 20px 0 20px;
    border-radius: 5px;
    z-index: 99999;
  }
  .up .img{
    position: absolute;
    left: 24px;
    top: -20px;
  }
  .up .title{
    margin-left: 90px;
  }
  .up header{
    height: 24px;
    border-bottom: 2px solid #D7D7D7;
    padding: 30px 0 10px 0;
    margin-left: 1%;
    margin-right: 10px;
    margin-bottom: 10px;
  }
  .up .header-title{
    height: 24px;
    display: inline-block;
    color: #FB7862;
    border-bottom: 2px solid #FB7862;
    padding: 0 10px 10px 10px;
    margin-left: 0;
  }
  .up .form{
    margin-left: 10%;
    margin-right: 10px;
  }
  .up .form .firstLine{
    padding: 30px 0 10px 0;
  }
  .up .form .secondLine{
    padding: 30px 0 50px 0;
  }
  .up .form .form-longText{
    width: 50%;
    border: 0;
    border-bottom: 1px solid #D7D7D7;
  }
  .up .form .form-text{
    width: 70%;
    border: 0;
    border-bottom: 1px solid #D7D7D7;
  }
  .up ul{
    margin-top: 30px;
    margin-left: 75px;
  }
  .up ul li{
    float: left;
  }
  .up .bottom{
    border-top: 1px solid #D7D7D7;
    margin-top: 20px;
    background: #F8F8F8;
    padding: 8px 0;
    margin-left: -20px;
    margin-right: -20px;
    border-radius: 0 0 5px 5px;
  }
  .up .bottom .right{
    text-align: right;
    padding: 0 10px 0 0;
  }
  .button{
    text-align: center;
  }
  .btn{
    cursor:pointer
  }
  .tipUp{
    width: 30%;
    position: fixed;
    top: 150px;
    left: 50%;
    margin-left: -15%;
    z-index: 99999;
    background: #FFF;
    padding: 20px 20px 20px 20px;
    border-radius: 5px;
  }
  .tipUp .row{
    text-align: center;
    padding-top: 15px;
  }
</style>
